#import pymel as pm
#pm.openFile('/home/ozgur/_test_scene.ma',force=True)

#
import maya.OpenMaya as OpenMaya

myObject = OpenMaya.MObject()
myTgtObj = OpenMaya.MObject()
myLocator = OpenMaya.MObject()

myDagIt = OpenMaya.MItDag()


while( not myDagIt.isDone() ):
        if myDagIt.partialPathName() == 'pCubeShape1':
                myObject = myDagIt.currentItem()
                
        if myDagIt.partialPathName() == 'locator1':
                myLocator = myDagIt.currentItem()
                
        if myDagIt.partialPathName() == 'locator2':
                myTgtObj = myDagIt.currentItem()
                
        myDagIt.next()

# locator
transformFn = OpenMaya.MFnTransform( myLocator )
worldPos = OpenMaya.MVector()
worldPos = transformFn.getTranslation( OpenMaya.MSpace.kTransform )

worldPosAsMPoint = OpenMaya.MPoint( worldPos )

closestPos = OpenMaya.MPoint()

# creating int pointer
su = OpenMaya.MScriptUtil()
su.createFromInt(0)
myIntPtr = su.asIntPtr()

# mesh
myMeshFn = OpenMaya.MFnMesh( myObject )
myMeshFn.getClosestPoint( worldPosAsMPoint, closestPos )

closestPosAsVect = OpenMaya.MVector ( closestPos )
transformFn = OpenMaya.MFnTransform( myTgtObj )
transformFn.setTranslation( closestPosAsVect, OpenMaya.MSpace.kTransform )
